const wxCharts = require('../../utils/wxcharts-min.js')
const util = require('../../utils/util.js')
var pieChart = null;
var lineChart = null;
let series = []

var dataTime = {
  dateTime: "",
  teamType: ""
}

Page({
  data: {
    array: [
      "请选择",
      "金融同业",
      "交易银行",
      "对私手机银行",
      "凤凰会"
    ],
    objectArray: [
      {
        "id": 0,
        "name": "请选择"
      }, {
        "id": 1,
        "name": "金融同业"
      }, {
        "id": 2,
        "name": "交易银行"
      }, {
        "id": 3,
        "name": "对私手机银行"
      }, {
        "id": 4,
        "name": "凤凰会"
      }
    ],
    index: 0,
    date: util.getNowFormatDate(),
    series: series
  },
  touchHandler: function (e) {
    console.log(pieChart.getCurrentDataIndex(e));
  },
  onLoad: function (e) {},
  onShow: function() {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/wxchat/superadmin/getAttendance',
      method: "POST",
      header: {
        'Content-Type': 'application/json' // 默认值
      },
      data: JSON.stringify(dataTime),
      success: function (res) {
        const data = res.data
        if (data.retCode) {
          console.log(JSON.stringify(data.attendance)),
          series = data.attendance,
          pie(series),
          line(series)
        }
      }
    })
  },
  bindPickerChange: function(e) {
    wx.request({
      url: 'http://localhost:8080/wxchat/superadmin/getAttendance',
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: JSON.stringify(dataTime),
      success: function (res) {
        const data = res.data
        console.log(data)
        if (data.retCode) {
          line(series)
        }
      }
    })
    console.log('picker222发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    wx.request({
      url: 'http://28.5.21.242:8080/wxchat/superadmin/getAttendance',
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: JSON.stringify(dataTime),
      success: function (res) {
        const data = res.data
        if (data.retCode) {
          pie(series)
        }
      }
    })
    this.setData({
      date: e.detail.value
    })
  },
});

function pie(series) {
  var windowWidth = 414;
  try {
    var res = wx.getSystemInfoSync();
    windowWidth = res.windowWidth;
  } catch (e) {
    console.error('getSystemInfoSync failed!');
  }
  pieChart = new wxCharts({
    animation: true,
    canvasId: 'pieCanvas',
    type: 'pie',
    series: series,
    width: windowWidth,
    height: 300,
    dataLabel: true,
  });
}

function line(series) {
  var windowWidth = 414;
  try {
    var res = wx.getSystemInfoSync();
    windowWidth = res.windowWidth;
  } catch (e) {
    console.error('getSystemInfoSync failed!');
  }
  lineChart = new wxCharts({
    canvasId: 'lineCanvas',
    type: 'line',
    categories: ['2012', '2013', '2014', '2015', '2016', '2017'],
    series: [{
      name: '成交量1',
      data: [0.15, 0.2, 0.45, 0.37, 0.4, 0.8],
      format: function (val) {
        return val.toFixed(2) + '万';
      }
    }, {
      name: '成交量2',
      data: [0.30, 0.37, 0.65, 0.78, 0.69, 0.94],
      format: function (val) {
        return val.toFixed(2) + '万';
      }
    }],
    yAxis: {
      title: '成交金额 (万元)',
      format: function (val) {
        return val.toFixed(2);
      },
      min: 0
    },
    width: 320,
    height: 200
  });
}


function getWidth () {
  wx.id
}